# -*- coding: utf-8 -*-


import numpy as np
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
import preprocess.bag as bag
import preprocess.CleanUp as CleanUp

class SVM:
    KERNEL_LINEAR='linear'
    KERNEL_SIGMOID='sigmoid'
    KERNEL_POLY='poly'
    KERNEL_RBF='rbf'

    classif=None
    def __init__(self):
        kernel=self.KERNEL_LINEAR
        self.classif = OneVsRestClassifier(SVC(kernel=kernel))
        pass

    def train(self, features, labels):
        self.classif.fit(features, labels)
        pass


    def validation(self, features, labels):
        predicted = self.classif.predict(features)
        return accuracy_score(predicted, labels)

    def cross_validation(self, features, labels, nfold=5):

        from sklearn.model_selection import cross_val_score
        from sklearn.model_selection import ShuffleSplit
        from sklearn import preprocessing
        from sklearn.pipeline import make_pipeline

        cv = ShuffleSplit(n_splits=nfold, test_size=.3, random_state=0)

        clf = make_pipeline(preprocessing.StandardScaler(), self.classif)

        scores = cross_val_score(clf, features, labels, cv=cv)
        return scores.mean()

    def load(self,filename):
        pass


    def save(self,filename):
        pass

if __name__ == '__main__':

    _feature_dict = ['a','c']

    features = [
        ['a','b'],
        ['b','c'],
        ['a']
    ]

    labels = [
        1,
        2,
        1
    ]

    classif = SVM()
    classif.train(features,labels)
    print classif.validation(features,labels)
    print classif.cross_validation(features,labels,5)
    pass